{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a34b2f46",
   "metadata": {},
   "source": [
    "# Pandas\n",
    "\n",
    "- Pandas是基于NumPy的一种工具，该工具是为了解决数据分析任务而创建的。Pandas纳入了大量库和一些标准的数据模型，提供了高效地操作大型数据集所需的工具。pandas提供了大量能使我们快速便捷地处理数据的函数和方法。\n",
    "  \n",
    "- Pandas基于两种数据类型：series-与dataframe。\n",
    "  - Series是Pandas中最基本的对象，Series是基于NumPy的数组对象建立的，与NumPy数组的不同之处在于，Series能为数据自定义标签，也就是索引(index),然后通过索引来访问数组中的据。\n",
    "\n",
    "  - DataFrame（数据表）是一种2维数据结构，数据以表格的形式存储，分成若干行和列。常见的操作包括选取、替换行或列的数据，重组数据表、修改索引、多重筛选等。可以把DataFrame理解成一组采用同样索引的Series的集合。调用DataFrame()可以将多种格式的数据转换为DataFrame对象，它的的三个参数data、index和columns分别为数据、行索引和列索引。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fec152f",
   "metadata": {},
   "source": [
    "## Series 一维数组\n",
    "\n",
    "### Seris的创建和对象获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "59c6530f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "3    4\n",
      "dtype: int64\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "dtype: int64\n",
      "e    1\n",
      "f    2\n",
      "g    3\n",
      "h    4\n",
      "dtype: int64\n",
      "0    0\n",
      "1    1\n",
      "2    2\n",
      "3    3\n",
      "4    4\n",
      "dtype: int64\n",
      "0    0\n",
      "1    1\n",
      "2    2\n",
      "3    3\n",
      "4    4\n",
      "5    5\n",
      "6    6\n",
      "7    7\n",
      "8    8\n",
      "9    9\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 创建Series对象\n",
    "# 如果省略index参数，会创建默认索引，索引值为[0,1,...,len(data)-1]\n",
    "seri1 = pd.Series([1,2,3,4]) \n",
    "print(seri1)\n",
    "\n",
    "# 自己创建索引\n",
    "# seril = pd.Series([1,2,3,4], index = ['a','b','c','d'])\n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "print(seri1)\n",
    "\n",
    "# 将dict转换为series\n",
    "data_dict = {'e':1,'f':2, 'g':3, 'h':4}\n",
    "seri1 = pd.Series(data_dict)\n",
    "print(seri1)\n",
    "\n",
    "# 将range(), np.arange() 转化为series\n",
    "seri1 = pd.Series(range(5))\n",
    "print(seri1)\n",
    "\n",
    "seri1 = pd.Series(np.arange(10))\n",
    "print(seri1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fb8eae01",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "Index(['a', 'b', 'c', 'd'], dtype='object')\n",
      "[('a', 1), ('b', 2), ('c', 3), ('d', 4)]\n",
      "[('a', 1), ('b', 2), ('c', 3), ('d', 4)]\n"
     ]
    }
   ],
   "source": [
    "# 获取series内容\n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "print(seri1.values)\n",
    "\n",
    "# 获取series索引\n",
    "print(seri1.index)\n",
    "\n",
    "# 获取series索引和值对\n",
    "print(list(seri1.items()))\n",
    "print(list(seri1.iteritems()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "0bcd6219",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "use location:  3\n",
      "use index:  3\n",
      "use index：  a    1\n",
      "c    3\n",
      "dtype: int64\n",
      "use location：  a    1\n",
      "c    3\n",
      "dtype: int64\n",
      "index slice：  a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n",
      "location slice：  a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 选取数据\n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "## 使用位置获取数据\n",
    "print('use location: ',seri1[2])\n",
    "## 使用索引获取数据\n",
    "print('use index: ',seri1['c'])\n",
    "\n",
    "## 获取多个数据\n",
    "print('use index： ',seri1[['a','c']])\n",
    "print('use location： ',seri1[[0,2]])\n",
    "\n",
    "# 使用切片获取数据\n",
    "print('index slice： ',seri1['a':'c'])  #左右都包含\n",
    "print('location slice： ',seri1[0:3])   # 做包含右不包含"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "107440cb",
   "metadata": {},
   "source": [
    "### 修改数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "548b9403",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "dtype: int64\n",
      "o    1\n",
      "p    2\n",
      "q    3\n",
      "r    4\n",
      "dtype: int64\n",
      "o     1\n",
      "p     2\n",
      "q     3\n",
      "r     4\n",
      "s    40\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 修改索引 \n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "print(seri1)\n",
    "seri1.index = list('opqr')\n",
    "print(seri1) \n",
    "\n",
    "# reindex \n",
    "# Conform Series to new index with optional filling logic.\n",
    "# 相同的index, 获取原值，原来没有的index, 填N.A.\n",
    "# seri1 = seri1.reindex(index = list('achfk'))   # N.A.\n",
    "# print(seri1)\n",
    "seri1 =seri1.reindex(index=list('opqrs'),fill_value=40)\n",
    "print(seri1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "575affef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "dtype: int64\n",
      "a      1\n",
      "b    100\n",
      "c      3\n",
      "d      4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 修改值\n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "print(seri1)\n",
    "seri1[1]=100\n",
    "print(seri1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2bf47d11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除值, drop\n",
    "seri1 = pd.Series(range(5),index = list('abcde'))\n",
    "print(seri1)\n",
    "seri1.drop(['a','b','c'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "201d80e2",
   "metadata": {},
   "source": [
    "### 计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "08a1f9fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BJ    1\n",
      "SH    2\n",
      "GZ    3\n",
      "HZ    4\n",
      "dtype: int64\n",
      "BJ    11\n",
      "SH    12\n",
      "JN    13\n",
      "WH    14\n",
      "dtype: int64\n",
      "BJ    12.0\n",
      "GZ     NaN\n",
      "HZ     NaN\n",
      "JN     NaN\n",
      "SH    14.0\n",
      "WH     NaN\n",
      "dtype: float64\n",
      "BJ    1\n",
      "SH    2\n",
      "GZ    3\n",
      "dtype: int64\n",
      "BJ    2\n",
      "SH    4\n",
      "GZ    6\n",
      "HZ    8\n",
      "dtype: int64\n",
      "BJ     1\n",
      "SH     4\n",
      "GZ     9\n",
      "HZ    16\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "seri1 = pd.Series([1,2,3,4],index=['BJ','SH','GZ','HZ']) \n",
    "seri2 = pd.Series([11,12,13,14],index=['BJ','SH','JN','WH']) \n",
    "\n",
    "print(seri1)\n",
    "print(seri2) \n",
    "print(seri1+seri2)\n",
    "\n",
    "# 过滤数据\n",
    "print(seri1[seri1 <= 3])\n",
    "# 标量乘法  \n",
    "print(seri1*2) \n",
    "# numpy 函数 \n",
    "print(np.square(seri1)) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c162e1cd",
   "metadata": {},
   "source": [
    "## DataFrame 二维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7c6029c",
   "metadata": {},
   "source": [
    "### DataFrame 创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "71e1cb32",
   "metadata": {},
   "outputs": [],
   "source": [
    "# data 数据\n",
    "# index 行索引\n",
    "# columns 列索引\n",
    "df = pd.DataFrame(data = np.random.randint(0,10,(4,4)),\n",
    "                 index = [1,2,3,4],\n",
    "                 columns = list('abcd'))\n",
    "\n",
    "df\n",
    "\n",
    "# 使用字典生成Dataframe\n",
    "# 字典的key为列索引 \n",
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "df2\n",
    "\n",
    "df3 = pd.DataFrame.from_dict(data_dict) \n",
    "df3\n",
    "\n",
    "# Series 转成DataFrame \n",
    "# 索引相同直接合并, 索引不同对应缺少值NaN \n",
    "data_dict2 = {'id': pd.Series(['101','102','103'],index = list('abc')),\n",
    "              'age': pd.Series([20,30,23,25], index = list('abcd')),\n",
    "              'height': pd.Series([176,159,165], index = list('abe'))\n",
    "}\n",
    "df4 = pd.DataFrame(data_dict2)\n",
    "df4\n",
    "\n",
    "# Dataframe转成字典\n",
    "df4.to_dict()\n",
    "# 列索引 外层字典的key \n",
    "# 行索引 内层字典的key\n",
    "\n",
    "# 建立空DataFrame\n",
    "df5 = pd.DataFrame(columns = list('abcd'))\n",
    "# df5.iloc[0]=[1,2,3,4]\n",
    "# df5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf1d960d",
   "metadata": {},
   "source": [
    "### DataFrame 基本属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "c0ec5c52",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n",
      "(4, 3)\n",
      "2\n",
      "['id', 'grade', 'height']\n",
      "['wang', 'may', 'john', 'kate']\n",
      "id        object\n",
      "grade      int64\n",
      "height     int64\n",
      "dtype: object\n",
      "[['101' 100 165]\n",
      " ['102' 90 198]\n",
      " ['103' 80 178]\n",
      " ['104' 95 168]]\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 4 entries, wang to kate\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   id      4 non-null      object\n",
      " 1   grade   4 non-null      int64 \n",
      " 2   height  4 non-null      int64 \n",
      "dtypes: int64(2), object(1)\n",
      "memory usage: 128.0+ bytes\n",
      "None\n",
      "            grade      height\n",
      "count    4.000000    4.000000\n",
      "mean    91.250000  177.250000\n",
      "std      8.539126   14.908052\n",
      "min     80.000000  165.000000\n",
      "25%     87.500000  167.250000\n",
      "50%     92.500000  173.000000\n",
      "75%     96.250000  183.000000\n",
      "max    100.000000  198.000000\n",
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "       id  grade  height\n",
      "john  103     80     178\n",
      "kate  104     95     168\n"
     ]
    }
   ],
   "source": [
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "print(df2.shape) #行列数\n",
    "print(df2.ndim) #维度\n",
    "\n",
    "print(df2.columns.tolist())  #列索引 \n",
    "print(df2.index.tolist())  # 行索引 \n",
    "\n",
    "print(df2.dtypes) #数据类型 \n",
    "print(df2.values) #提取数据\n",
    "\n",
    "print(df2.info()) # 数据信息 \n",
    "print(df2.describe())  # 对数值变量的一些分布信息\n",
    "\n",
    "print(df2.head(2)) # 头部, 默认5行数据,  \n",
    "print(df2.tail(2)) # 尾部, "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccc7f704",
   "metadata": {},
   "source": [
    "### 获取行列的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "48cc0acf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n",
      "wang    100\n",
      "may      90\n",
      "john     80\n",
      "kate     95\n",
      "Name: grade, dtype: int64\n",
      "<class 'pandas.core.series.Series'>\n",
      "wang    100\n",
      "may      90\n",
      "john     80\n",
      "kate     95\n",
      "Name: grade, dtype: int64\n",
      "      grade  height\n",
      "wang    100     165\n",
      "may      90     198\n",
      "john     80     178\n",
      "kate     95     168\n",
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "       id  grade  height\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "       id  grade\n",
      "may   102     90\n",
      "john  103     80\n"
     ]
    }
   ],
   "source": [
    "# 方法1：基础索引，直接引用，使用df[]\n",
    "# NOTE: df[] 只能进行列选择，或行选择，不能同时放入多行多列选择\n",
    "\n",
    "# 列\n",
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "print(df2['grade'])  # 获取一列，得到的对象为Series\n",
    "print(type(df2['grade']))\n",
    "\n",
    "print(df2.grade)\n",
    "\n",
    "# 获取多列\n",
    "print(df2[['grade','height']]) #得到的对象为DataFrame\n",
    "\n",
    "# 获取一行\n",
    "print(df2[0:1])\n",
    "# 获取多行\n",
    "print(df2[1:3])\n",
    "\n",
    "# 获取多行多列\n",
    "print(df2[1:3][['id','grade']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "id": "105effbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "wang    100\n",
       "may      90\n",
       "john     80\n",
       "kate     95\n",
       "Name: grade, dtype: int64"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2：使用df.loc[]， 通过标签索引获得数据 \n",
    "\n",
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "# 获取一行数据\n",
    "df2.loc['john']\n",
    "\n",
    "#获取某一行列位置的数据\n",
    "df2.loc['john','grade']\n",
    "\n",
    "#获取一行，多列数据\n",
    "df2.loc['john',['grade','height']]\n",
    "\n",
    "# 获取多行多列\n",
    "df2.loc[['john','may'],['grade','height']]\n",
    "df2.loc['wang':'john',:]\n",
    "\n",
    "# 获取一列\n",
    "df2.loc[:,'grade']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "id": "db1ec483",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "id        104\n",
       "grade      95\n",
       "height    168\n",
       "Name: kate, dtype: object"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法3：使用df.iloc[]， 通过位置获得数据\n",
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "# 获取一行\n",
    "df2.iloc[0]\n",
    "\n",
    "# 获取多行\n",
    "df2.iloc[0:3]\n",
    "df2.iloc[[0,3]]\n",
    "\n",
    "# 获取一列\n",
    "df2.iloc[:,2]\n",
    "\n",
    "# 某一个值 \n",
    "df2.iloc[1,0]\n",
    "\n",
    "# 最后一行\n",
    "df2.iloc[-1,:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57535b32",
   "metadata": {},
   "source": [
    "### 修改元素值和排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "4ea7693b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>grade</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>wang</th>\n",
       "      <td>101</td>\n",
       "      <td>100</td>\n",
       "      <td>165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>john</th>\n",
       "      <td>103</td>\n",
       "      <td>99</td>\n",
       "      <td>178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>kate</th>\n",
       "      <td>104</td>\n",
       "      <td>95</td>\n",
       "      <td>168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>may</th>\n",
       "      <td>102</td>\n",
       "      <td>90</td>\n",
       "      <td>198</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       id  grade  height\n",
       "wang  101    100     165\n",
       "john  103     99     178\n",
       "kate  104     95     168\n",
       "may   102     90     198"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改 \n",
    "df2.loc['john','grade'] = 99\n",
    "df2\n",
    "\n",
    "# 排序 \n",
    "# by 字段 列 \n",
    "# ascending 默认排序方式：升序 , 降序 ascending= False\n",
    "df2.sort_values(by='grade',ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5eaeaa1",
   "metadata": {},
   "source": [
    "### 修改index, columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "19154b1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n",
      "Index(['wang', 'may', 'john', 'kate'], dtype='object')\n",
      "      o_cupl  p_cupl  q_cupl\n",
      "a2022    101     100     165\n",
      "b2022    102      90     198\n",
      "c2022    103      80     178\n",
      "d2022    104      95     168\n",
      "        id  p_cupl  q_cupl\n",
      "a      101     100     165\n",
      "b2022  102      90     198\n",
      "c2022  103      80     178\n",
      "d2022  104      95     168\n",
      "             id  p_cupl  q_cupl\n",
      "student_id                     \n",
      "101         101     100     165\n",
      "102         102      90     198\n",
      "103         103      80     178\n",
      "104         104      95     168\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>104</th>\n",
       "      <th>95</th>\n",
       "      <th>168</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>student_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>101</td>\n",
       "      <td>100</td>\n",
       "      <td>165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>102</td>\n",
       "      <td>90</td>\n",
       "      <td>198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>103</th>\n",
       "      <td>103</td>\n",
       "      <td>80</td>\n",
       "      <td>178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>104</td>\n",
       "      <td>95</td>\n",
       "      <td>168</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            104   95  168\n",
       "student_id               \n",
       "101         101  100  165\n",
       "102         102   90  198\n",
       "103         103   80  178\n",
       "104         104   95  168"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "# 直接替换 index，columns\n",
    "print(df2.index)\n",
    "df2.index = list('abcd')\n",
    "df2\n",
    "\n",
    "df2.columns = list('opq')\n",
    "df2\n",
    "\n",
    "# 使用rename\n",
    "# 传入函数，调整索引规则, 类似于map的作用\n",
    "# index = index_adjust, 会将每个行索引依次传入这个函数中做调整\n",
    "def index_adjust(x):  # 使用函数，自定义调整规则\n",
    "    #print(x)\n",
    "    return x + '2022'\n",
    "def columns_adjust(x):\n",
    "    return x + '_cupl'\n",
    "df2.rename(index = index_adjust, columns = columns_adjust, inplace = True)  \n",
    "# inplace: 默认为False, 不在原dataframe上修改，返回新的dataframe; True: 在原dataframe上修改\n",
    "print(df2)\n",
    "\n",
    "# rename 也可以传入字典，更改某个或某几个index 或columns\n",
    "df2.rename(index={'a2022':'a'},columns= {'o_cupl': 'id'},inplace= True)\n",
    "print(df2)\n",
    "\n",
    "# 将列转化为索引, 指定一列为索引\n",
    "df2.set_index('id', inplace=True,drop=False)\n",
    "df2.index.name = 'student_id'\n",
    "print(df2)\n",
    "\n",
    "# 将行索引转换为列\n",
    "df2.reset_index()  # drop = True: delete index\n",
    "\n",
    "\n",
    "# 将行转化为列名\n",
    "df3 = df2.set_axis(df2.iloc[3],axis = 1)\n",
    "df3.columns.name = None\n",
    "df3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29c8068e",
   "metadata": {},
   "source": [
    "## 行、列遍历"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dac97891",
   "metadata": {},
   "source": [
    "- iterrows(): 按行遍历，将DataFrame的每一行迭代为(index, Series)对\n",
    "  - 每一行的内容为一个Series, 索引为列名, 可以通过列名对元素进行访问。\n",
    "- itertuples(): 按行遍历，将DataFrame的每一行迭代为元祖\n",
    "  - 使用`getattr(row, 列名)` 获取元素值\n",
    "  \n",
    "- iteritems():按列遍历，将DataFrame的每一列迭代为(列名, Series)对，可以通过row[index]对元素进行访问。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ea7a5fd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n",
      "id\n",
      "wang    101\n",
      "may     102\n",
      "john    103\n",
      "kate    104\n",
      "Name: id, dtype: object\n",
      "***\n",
      "grade\n",
      "wang    100\n",
      "may      90\n",
      "john     80\n",
      "kate     95\n",
      "Name: grade, dtype: int64\n",
      "***\n",
      "height\n",
      "wang    165\n",
      "may     198\n",
      "john    178\n",
      "kate    168\n",
      "Name: height, dtype: int64\n",
      "***\n"
     ]
    }
   ],
   "source": [
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "# for index,row in df2.iterrows():\n",
    "#     print(index)\n",
    "#     print(row)\n",
    "#     print('***')\n",
    "\n",
    "# print('-'*10)\n",
    "# for row in df2.itertuples():\n",
    "#     print(row)\n",
    "#     print(getattr(row, 'id'), getattr(row, 'height'))\n",
    "\n",
    "\n",
    "for col_name, col in df2.iteritems():\n",
    "    print(col_name)\n",
    "    print(col)\n",
    "    print('***')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
